<!--
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 * No deletion without permission, or be held responsible to law.
 * @author YDZ
-->
<template>
  <div>
    <BasicTable @register="registerTable">
      <template #tableTitle>
        <Icon :icon="getTitle.icon" class="m-1 pr-1" />
        <span> {{ getTitle.value }} </span>
      </template>
      <!-- <template #toolbar> 
        <a-button type="primary" @click="handleRePush({})" v-auth="'btn:reset:tstbz'">
          <Icon icon="ant-design:undo-outlined" /> {{ t('重置推送状态') }}
        </a-button>
      </template> -->
      <template #firstColumn="{ record }">
        <a @click="handleForm({ id: record.id })">
          {{ record.djno }}
        </a>
      </template>
      <template #projectno="{ record }">
        <!-- <a @click="handleDetailsForm(record)">
          <span style="text-decoration: underline; margin-right: 5px">查看</span>
        </a>
        {{ record.projectno }} -->
        <a @click="handleDetailsForm(record)">
          {{ record.projectno }}
        </a>
      </template>
      <!-- 勾选 -->
      <template #checkBox="{ record, column }">
        <!-- {{ record[column.dataIndex[0]] }}
        {{ [column.dataIndex[1]] }} -->
        <a-checkbox
          :checked="record[column.dataIndex[0]][column.dataIndex[1]] == '1' ? true : false"
        />
      </template>
    </BasicTable>
    <InputForm @register="registerDrawer" @success="handleSuccess" />
    <SupplyInputForm @register="registerSupplyDrawer" @success="handleSuccess" />
  </div>
</template>
<script lang="ts">
  export default defineComponent({
    name: 'ViewsPmCompletAcceptCheckList',
  });
</script>
<script lang="ts" setup>
  import { defineComponent, onMounted } from 'vue';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { useMessage } from '/@/hooks/web/useMessage';
  import { router } from '/@/router';
  import { Icon } from '/@/components/Icon';
  import { BasicTable, BasicColumn, useTable } from '/@/components/Table';
  import { pmPreacceptanceListDataJG } from '/@/api/accept/pre/pmPreacceptance';
  import { useDrawer } from '/@/components/Drawer';
  import { FormProps } from '/@/components/Form';
  import InputForm from './form.vue';
  // 引入预验收表单
  import SupplyInputForm from './../../supply/pmWatersupply/form.vue';
  import { useUserStore } from '/@/store/modules/user';
  import {
    pmProjectinformationRePush,
    pmProjectinformationBackPush,
  } from '/@/api/proj/info/pmProjectinformation';

  const userStore = useUserStore();

  const { t } = useI18n('accept.pre.pmPreacceptance');
  const { showMessage } = useMessage();
  const getTitle = {
    icon: router.currentRoute.value.meta.icon || 'ant-design:book-outlined',
    value: router.currentRoute.value.meta.title || t('竣工验收发起'),
  };

  const searchForm: FormProps = {
    baseColProps: { lg: 6, md: 8 },
    labelWidth: 120,
    schemas: [
      {
        label: t('工程编号'),
        field: 'viewCode',
        component: 'Input',
      },
      {
        label: t('工程名称'),
        field: 'projectname',
        component: 'Input',
      },
      // {
      //   label: t('验收方式'),
      //   field: 'canfilecheck',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'zf_ys_type',
      //   },
      // },
      {
        label: t('验收结果'),
        field: 'checkresult',
        component: 'Input',
      },
      // {
      //   label: t('是否竣工完成'),
      //   field: 'acceptCheck.jgysStatus',
      //   component: 'Select',
      //   componentProps: {
      //     dictType: 'sys_yes_no',
      //   },
      // },
    ],
  };

  const tableColumns: BasicColumn[] = [
    {
      title: t('项目编号'),
      dataIndex: 'projectno',
      key: 'a.projectno',
      sorter: true,
      width: 180,
      align: 'left',
      slot: 'projectno',
      fixed: 'left',
    },
    {
      title: t('项目名称'),
      dataIndex: 'projectname',
      key: 'a.name',
      sorter: true,
      width: 280,
      align: 'left',
    },
    {
      title: t('项目负责人'),
      dataIndex: 'pmZone.leaderUser.userName',
      key: 'pmZone.projectleader',
      sorter: true,
      width: 280,
      align: 'left',
    },
    // {
    //   title: t('验收方式'),
    //   dataIndex: 'canfilecheck',
    //   key: 'a.canfilecheck',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   dictType: 'zf_ys_type',
    // },
    {
      title: t('验收结果'),
      dataIndex: 'checkresult',
      key: 'a.checkresult',
      sorter: true,
      width: 130,
      align: 'center',
    },
    {
      title: t('整改完成'),
      dataIndex: 'acceptCheck.zgStatus',
      key: 'accept.zg_status',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('废管处理'),
      dataIndex: 'acceptCheck.fgStatus',
      key: 'accept.fg_status',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否需中间验收'),
      dataIndex: 'acceptCheck.bneedMid',
      key: 'accept.bneedMid',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('中间验收(条件)'),
      dataIndex: 'acceptCheck.zjysStatus',
      key: 'accept.zjys_status',
      sorter: true,
      width: 120,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('材料核对'),
      dataIndex: 'acceptCheck.bmatCheck',
      key: 'accept.bmatcheck  ',
      sorter: true,
      width: 100,
      align: 'center',
      slot: 'checkBox',
    },
    {
      title: t('是否通水推送'),
      dataIndex: 'acceptCheck.bpushGw',
      key: 'accept.bpushGw',
      sorter: true,
      width: 130,
      align: 'center',
      slot: 'checkBox',
    },
    // {
    //   title: t('推送管网'),
    //   dataIndex: 'acceptCheck.bpushGw',
    //   key: 'accept.bpushGw',
    //   sorter: true,
    //   width: 130,
    //   align: 'center',
    //   slot: 'checkBox',
    // },
    {
      title: t('是否满足竣工条件'),
      dataIndex: 'acceptCheck.canover',
      key: 'accept.canover',
      sorter: false,
      width: 150,
      dictType: 'zf_jg_canOver',
      align: 'center',
      fixed: 'right',
    },
  ];

  const actionColumn: BasicColumn = {
    width: 180,
    align: 'left',
    actions: (record: Recordable) => [
      {
        // icon: 'ant-design:undo-outlined',
        color: 'success',
        title: t('撤回通水'),
        label: '撤回通水',
        popConfirm: {
          title: t('是否确认撤回通水'),
          confirm: handleBackPush.bind(this, record),
        },
        auth: 'btn:back:tongshui',
      },
      {
        // icon: 'ant-design:send-outlined',
        title: t('上传验收结果'),
        label: '上传验收结果',
        onClick: handleInitiate.bind(this, {
          projectname: record.projectname,
          projectno: record.projectno,
          checkresult: record.checkresult,
        }),
        auth: 'btn:add:jungong',
        ifShow: record.acceptCheck.canover == 1 && record.acceptCheck.jgysStatus != 1,
      },
    ],
  };
  const [registerDrawer, { openDrawer }] = useDrawer();
  const [registerSupplyDrawer, { openDrawer: openSupplyDrawer }] = useDrawer();
  const [registerTable, { reload, getForm, getSelectRows, getSelectRowKeys }] = useTable({
    api: pmPreacceptanceListDataJG,
    beforeFetch: (params) => {
      params.pageSize = 10000;
      return params;
    },
    columns: tableColumns,
    actionColumn: actionColumn,
    formConfig: searchForm,
    showTableSetting: true,
    useSearchForm: true,
    canResize: true,
    immediate: false,
    pagination: false,
    rowSelection: {
      type: 'checkbox',
    },
  });

  function handleDetailsForm(record: Recordable) {
    router.push({
      path: '/proj/info/pmProjectinformation/detailsForm',
      query: {
        id: record.projInfo.id,
        code: record.projectno,
        tabTitle: `项目详情(${record.projectno})`,
      },
    });
  }

  function handleForm(record: Recordable) {
    openDrawer(true, record);
  }

  async function handleDelete(record: Recordable) {
    const res = await pmPreacceptanceDelete(record);
    showMessage(res.message);
    handleSuccess();
  }

  // 发起竣工
  async function handleInitiate(record: Recordable) {
    console.log(record, '发起竣工');
    openSupplyDrawer(true, record);
  }

  // 默认查询条件
  onMounted(async () => {
    handleProjecte();
  });

  async function handleProjecte() {
    await getForm().setFieldsValue({
      viewCode: userStore.getProjecte.code,
      acceptCheck: {
        jgysStatus: '0',
      },
    });
    reload();
    await getForm().setFieldsValue({
      viewCode: userStore.getProjecte.code,
      acceptCheck: {
        jgysStatus: '0',
      },
    });
  }

  function handleSuccess() {
    reload();
  }

  // 重新推送
  async function handleRePush() {
    if (getSelectRowKeys().length) {
      let arr = await getSelectRows();
      let arrNew = arr.map((item) => {
        return item.projInfo.id;
      });
      const res = await pmProjectinformationRePush({ selIds: arrNew.join(',') });
      showMessage(res.message);
      handleSuccess();
    } else {
      showMessage('请先选择一行数据!');
    }
  }

  async function handleBackPush(record: Recordable) {
    const res = await pmProjectinformationBackPush({ selIds: record.projInfo.id });
    showMessage(res.message);
    handleSuccess();
  }
</script>
